package com.traffic.api.restful;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;

import org.codehaus.jettison.json.JSONObject;

import com.gxhl.api.core.model.GDPResponse;
import com.gxhl.api.core.tools.JerseyClient;
import com.sun.jersey.api.client.ClientResponse;
import com.traffic.api.config.ApiProperties;
import com.traffic.api.exception.ApiBizException;

@Path("/{v}/product")
public class ProductRestful extends CheckRestful {
	private static final String HOST_PATH = ApiProperties.FLOW_HOST + "rest/1.0/product";

	@POST
	@Consumes({ "application/json" })
	@Produces({ "application/json" })
	public GDPResponse getProductList(JSONObject param, @PathParam("v") String v) {
		GDPResponse result = new GDPResponse();
		result.setCode("0000");
		try {
			check(param,"status");

			ClientResponse response = JerseyClient.getInstance().post(HOST_PATH, param);

			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
			}

			GDPResponse output = (GDPResponse) response.getEntity(GDPResponse.class);

			result.setMsg(output.getMsg());
			result.setData(output.getData());
		} catch (ApiBizException e) {
			result.setCode("0001");
			result.setMsg(e.getCause().getMessage());
		} catch (WebApplicationException e) {
			result.setCode("0001");
			result.setMsg(e.getCause().getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("0001");
			result.setMsg("系统内部异常");
		}
		return result;
	}
}